// pages/map/map.js
var api = require("../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,//任务类型
    markers:[],//标记点数组
    taskList: "",//任务数据列表
    taskInfoIsHide: true,//任务信息框是否隐藏
    curTaskId: 0,//当前选中的任务ID
    curIsReceived:0,//当前选中的任务是否已接
    curTaskLongitude: 0,//当前选中任务经度
    curTaskLatitude: 0,//当前选中任务纬度
    curTaskDistance: "",//当前选中任务距离
    curTaskAddress: "",//当前选中任务地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    // 设置任务类型
    that.setData({
      type: type
    }, () => {
      // 获取任务列表
      that.getTaskList();
    });

    // 获取地图组件对象
    that.mapCtx = wx.createMapContext('map', that);
    // 定位到当前位置
    that.setLocation();

    var contex = wx.createCanvasContext('firstCanvas')
    contex.beginPath(); //开始绘制
    contex.arc(100, 100, 100, 0, Math.PI * 2, false);
    contex.clip();
    contex.draw();
  },

  // 获取任务列表
  getTaskList: function () {
    var that = this;
    wx.showLoading({
      title:"加载中"
    });//显示加载中
    api.getTaskList(that.data.type).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var data = res.data;
        // 设置标记点
        var markers=[];
        for (var i = 0; i < data.length; i++) {
          var d = data[i];
          // 设置标记点图标
          var iconPath = "../../resources/images/icon/map/dibiao.png";
          // 如果已接
          if (d.isReceived == 1) {
            iconPath = "../../resources/images/icon/map/dibiao_my.png";
          }
          markers.push({
            id: i,
            longitude: d.longitude,
            latitude: d.latitude,
            iconPath: iconPath,
            width: "40px",
            height: "40px"
          });
        }
        that.setData({
          markers: markers,
          taskList: data
        }, () => {
          // 处理空坐标问题
          that.setNullCoord();
        });
      }
    });
  },

  // 处理空坐标问题
  setNullCoord: function () {
    var that = this;
    var taskList = that.data.taskList;
    for (var i = 0; i < taskList.length; i++) {
      var task = taskList[i];
      if ((!task.longitude) || task.longitude == 0 || task.longitude == "") {
        //根据位置名称获取坐标
        that.getLocationByName(i, task.address);
      }
    }
  },

  //根据位置名称获取坐标
  getLocationByName: function (index, address) {
    var that = this;
    //根据位置名称获取坐标
    api.getLocationByName(address).then(res => {
      var longitude = res.result.location.lng;
      var latitude = res.result.location.lat;
      // 设置标记点
      var markers = that.data.markers;
      markers[index].longitude = longitude;
      markers[index].latitude = latitude;
      that.setData({
        markers: markers
      });
    });
  },
  
  // 定位到当前位置
  setLocation:function(){
    var that = this;
    that.mapCtx.moveToLocation();
  },

  // 标记点点击事件
  markerClick: function (res) {
    var that = this;
    //获得当前选中的任务数据
    var task = that.data.taskList[res.markerId];
    var curTaskId = task.outvisitId;
    var curIsReceived = task.isReceived;
    var curTaskLongitude = task.longitude;
    var curTaskLatitude = task.latitude;
    var curTaskDistance = (parseInt(task.distance) / 1000).toFixed(2);
    var curTaskAddress = task.address;
    that.setData({
      curTaskId: curTaskId,
      curIsReceived: curIsReceived,
      curTaskLongitude: curTaskLongitude,
      curTaskLatitude: curTaskLatitude,
      curTaskDistance: curTaskDistance,
      curTaskAddress: curTaskAddress
    });
    that.taskInfoShowOrHide();
  },

  // 查看任务详情
  taskDetails:function(){
    var that = this;
    var type;
    if (that.data.curIsReceived == 1) {//如果已接
      type = 1;
    } else {//如果未接
      type = 2;
    }
    wx.navigateTo({
      url: '../outvisit/task/taskDetails?type=' + type + '&id=' + that.data.curTaskId + '&taskLongitude=' + that.data.curTaskLongitude + '&taskLatitude=' + that.data.curTaskLatitude + '&taskAddress=' + that.data.curTaskAddress
    });
  },

  // 跳转导航
  navigation: function () {
    var that = this;
    wx.openLocation({
      longitude: that.data.curTaskLongitude,
      latitude: that.data.curTaskLatitude,
      name: that.data.curTaskAddress
    });
  },

  // 任务信息框显示与隐藏
  taskInfoShowOrHide: function () {
    var taskInfoIsHide = this.data.taskInfoIsHide;
    if (taskInfoIsHide) {
      taskInfoIsHide = false;
    } else {
      taskInfoIsHide = true;
    }
    this.setData({
      taskInfoIsHide: taskInfoIsHide
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})